package cn.zqh.brand.rabbitmq;

import cn.hutool.json.JSONObject;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.brand.config.BSException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: zqh
 * @Date: 2023-03-15-13:47
 * @Description: rabbitmq服务$
 */
@Component
public class MyConsumer1 {

    @Autowired
    private RestTemplate restTemplate;

    @RabbitListener(queues = "ms_queue7")
    public void process(Message message){
        byte[] body = message.getBody();
        JSONObject entries = new JSONObject(body);
        String commodity = entries.getStr("commodityId");
        Integer number = entries.getInt("number");
        CommonResult commonResult = restTemplate.postForObject("http://127.0.0.1:9801/commodity/changeItemInventory?commodityId=" + commodity + "&number=" + number, "", CommonResult.class);
//        if (commonResult.getCode() != 200) {
//            throw new BSException(ResultCode.AnErrorOccurredInTheRemoteCall.getCode(),ResultCode.AnErrorOccurredInTheRemoteCall.getMessage());
//        }
        System.out.println("商品库存减少成功");
    }

}
